package com.xjzd.practice.statemachine;

public class StateMachine {

    public static OrderState transition(OrderState currentState, OrderEvent event) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Transition[] annotations = currentState.getClass().getField(currentState.name()).getAnnotationsByType(Transition.class);
        for (Transition anno : annotations) {
            if (anno.event() == event) {
                Action action = anno.action().newInstance();
                action.execute();
                return anno.target();
            }
        }

        throw new IllegalStateException("非法转换状态: " + currentState.getDesc() + " 不能执行 " + event.getName());
    }
}
